define([
    "app"
], function (app) {
    app.controller("attendanceNumberController", [
        "$scope",
        "$q",
        "$state",
        "attendanceNumberDataService",
        function ($scope, $q, $state, attendanceNumberDataService) {

            $scope.state = 'loading';

            attendanceNumberDataService
                .getTeamAttendanceInfo()
                .then(
                    function success(resp) {
                        $scope.state = 'finished';

                        $scope.teams = resp.data;
                        $scope.currentTeam = null;

                        $scope.teams.forEach(function (e, i, arr) {
                            e.members.forEach(function (e1, i1, arr1) {
                                e1.isAttendanceNumberExists = !!e1.attendanceNumber;
                            });
                        });

                        $scope.selectTeam = function (team) {
                            $scope.currentTeam = team;
                        };

                        $scope.checkAndSubmit = function () {

                            var a = $scope.currentTeam.members;
                            var tempArr = [];

                            // 检查成员间的学号是否有出现重复的
                            for (let i = 0; i < a.length; i++) {
                                tempArr.push(parseInt(a[i].attendanceNumber))
                            }
                            tempArr.sort((a, b) => a - b);

                            for (let i = 0; i < a.length - 1; i++) {
                                if (tempArr[i] === tempArr[i + 1]) {
                                    alert("成员间学号重复");
                                    return;
                                }
                            }

                            // 创建上传的数据
                            var submitData = [];
                            a.forEach(function (element, index) {
                                // 已经有考勤号的不写入
                                if (!element.isAttendanceNumberExists) {
                                    submitData.push({
                                        studentId: element.studentId,
                                        attendanceNumber: parseInt(element.attendanceNumber)
                                    });
                                }

                            });

                            attendanceNumberDataService
                                .submit(submitData)
                                .then(
                                    function success(resp) {
                                        // 没有重复项目
                                        if (resp.data.length === 0) {
                                            $state.go($state.current, {}, {reload: true});
                                        }
                                        // 有考勤号重复
                                        else {
                                            var mes = "以下成员的考勤号已被使用\n";

                                            resp.data.forEach(function (element, index) {
                                                mes += element.studentId + "=>" + element.attendanceNumber + "\n";
                                            });
                                            alert(mes);
                                        }
                                    },
                                    function error() {
                                        alert("似乎出了点问题!!!∑(ﾟДﾟノ)ノ");
                                        $state.go($state.current, {}, {reload: true});
                                    }
                                )
                        };
                    },
                    function error(resp) {

                        $scope.state = 'error';
                    }
                )
        }
    ])
});